package com.karson.service.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.karson.common.constants.Constants;
import com.karson.common.model.AlbumInfo;
import com.karson.common.model.WatchLater;
import com.karson.common.response.AlbumResponse;
import com.karson.common.response.CommentResponse;
import com.karson.common.response.CursorPage;
import com.karson.common.response.WatchLaterResponse;
import com.karson.service.service.AlbumInfoService;
import com.karson.service.service.WatchLaterService;
import com.karson.service.dao.WatchLaterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author ZKS
* @description 针对表【ar_watch_later】的数据库操作Service实现
* @createDate 2025-10-12 19:55:18
*/
@Service
public class WatchLaterServiceImpl extends ServiceImpl<WatchLaterMapper, WatchLater>
    implements WatchLaterService{
    @Autowired
    private WatchLaterMapper watchLaterMapper;
    @Autowired
    private AlbumInfoService albumInfoService;
    @Override
    public WatchLaterResponse offsetList(String offset) throws JsonProcessingException {
        int uid = StpUtil.getLoginIdAsInt();
        Integer lastId = null;
        if (offset != null) {
            // 以id作为游标
            String json = new String(Base64.getDecoder().decode(offset));
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, Object> map = objectMapper.readValue(json, Map.class);
            lastId = map.get("id") != null ? ((Number) map.get("id")).intValue() : null;
        }
        List<WatchLater> watchLater = watchLaterMapper.offsetList(uid,lastId);
        if (watchLater.isEmpty()) {
            return null;
        }
        CursorPage page = new CursorPage();
        page.setBegin(offset == null);
        page.setEnd(watchLater.size() < Constants.PAGE_SMALL_SIZE);
        // 构建下一页 offset
        if (!watchLater.isEmpty()) {
            Map<String, Object> nextOffsetMap = getNextOffsetMap(watchLater);
            String nextOffsetJson = new ObjectMapper().writeValueAsString(nextOffsetMap);
            page.setNextOffset(Base64.getEncoder().encodeToString(nextOffsetJson.getBytes(StandardCharsets.UTF_8)));
        }
        List<Integer> albumIds = watchLater.stream().map(WatchLater::getAlbumId).toList();
        LambdaQueryWrapper<AlbumInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AlbumInfo::getId, albumIds);
        List<AlbumInfo> albumInfoList = albumInfoService.list(queryWrapper);
        // 构建响应对象
        WatchLaterResponse watchLaterResponse = new WatchLaterResponse();
        watchLaterResponse.setCursor(page);
        watchLaterResponse.setAlbumInfos(albumInfoList);
        return watchLaterResponse;
    }


    private static Map<String, Object> getNextOffsetMap(List<WatchLater> responsePage) {
        WatchLater next = responsePage.get(responsePage.size() - 1);
        Map<String, Object> nextOffsetMap = new HashMap<>(2);
        nextOffsetMap.put("id", next.getId());
        return nextOffsetMap;
    }
}




